﻿@inject IHttpService _httpService

@inject IDataCacheService _dataCacheService
@inject IEventService _eventService
@inject IPopupService PopupService

<MDCard Class="@($"comment-reply-card pa-4 pb-0 pt-3 mb-3  {(IsChild?"elevation-2":"")}")" Color="@(IsChild?"primary":"background")">
    @if (string.IsNullOrWhiteSpace(_dataCacheService.UserInfor.Id) == false)
    {
            <CnGalWebSite.Shared.MasaComponent.Shared.Cards.Users.InfoCard Model="_dataCacheService.UserInfor" Outline/>
    }

    @if (_dataCacheService.ThemeSetting.IsCommentUseMarkdown)
    {
        <CnGalWebSite.Shared.MasaComponent.Shared.Components.Markdown.MasaMarkdown @bind-Context="@Text" Mode="MarkdownEditorMode.wysiwyg" Class="mb-4 mt-4" />
    }
    else
    {
        <MTextarea Rows="3"  Placeholder="请在留言板中友善发言，理性讨论"
                   @bind-Value="Text">
        </MTextarea>
    }

    <div style="display: flex !important; flex-wrap: wrap; gap: 1rem;">
        <CnGalWebSite.Components.Buttons.MasaButton Text="发表评论" IsAsync="true" OnClick="OnComment" Icon="mdi-check" Rounded />
         <CnGalWebSite.Components.Buttons.MasaButton Text="切换编辑器" OnClick="OnSwitch" Icon="mdi-circle-edit-outline " Rounded />
         <CnGalWebSite.Components.Images.UploadButton Text="上传图片" OnImageUploaded="OnImageUploaded" ShowPreview="false" Rounded />
    </div>
</MDCard>
<CnGalWebSite.Components.Verifications.DeviceIdentification @ref="deviceIdentification" />


@code {

    [Parameter]
    public bool IsChild { get; set; }

    [Parameter]
    public CommentType Type { get; set; }

    [Parameter]
    public string ObjectId { get; set; }

    [Parameter]
    public EventCallback OnRefresh { get; set; }

    CnGalWebSite.Components.Verifications.DeviceIdentification deviceIdentification;


    string image = "";
    string userId = "";
    string userName = "";

    public string Text { get; set; }
    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }


    public async Task OnComment()
    {
        //上传
        try
        {
            if (string.IsNullOrWhiteSpace(Text))
            {
                await PopupService.ToastErrorAsync("发表评论失败", "你还什么都没说呢，我们不能发表空的评论哦");
                return;
            }
            var obj = await _httpService.PostAsync<PublishCommentModel, Result>("api/comments/PublishComment", new PublishCommentModel { ObjectId = ObjectId, Text = Text, Type = Type,
                    Identification = await deviceIdentification.GetDeviceIdentificationModel()
            });
            //判断结果
            if (obj.Successful == false)
            {
                await PopupService.ToastErrorAsync("发表评论失败", obj.Error);
            }
            else
            {
                Text = "";
                await PopupService.ToastSuccessAsync("发表评论成功", "我们需要对你的评论进行审核，在通过审核之前，你的评论仅自己可见");
                await OnRefresh.InvokeAsync();
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "发表评论失败");
        }
    }


    public void OnSwitch()
    {
        _dataCacheService.ThemeSetting.IsCommentUseMarkdown = !_dataCacheService.ThemeSetting.IsCommentUseMarkdown;
        _eventService.OnSavaTheme();

        StateHasChanged();
        
    }

    public void OnImageUploaded(string path)
    {
        Text +=$"![]({path})" ;
        StateHasChanged();
    }
}
